package ecom.shell.transaction;

import java.io.PrintStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.resource.NotSupportedException;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.RollbackException;
import javax.transaction.SystemException;
import javax.transaction.UserTransaction;
import shell.ShellCommand;
import shell.ShellContext;

public class CommitCurrentTxCommandImpl implements ShellCommand {

    private ShellContext context = null;

    public CommitCurrentTxCommandImpl(ShellContext context) {
        this.context = context;
    }

    @Override
    public String getName() {
        return "commit";
    }

    @Override
    public String getUsage() {
        return "commit";
    }

    @Override
    public String getShortDescription() {
        return "commit the current transaction.\n";
    }

    @Override
    public String execute(String cmdline, PrintStream out, PrintStream err) {

        UserTransaction utx = (UserTransaction) context.getVar("userTransaction");
        try {
            utx.commit();
            context.setVar("userTransaction", utx);
            out.println("Commit the current transaction");
        } catch (RollbackException ex) {
            Logger.getLogger(CommitCurrentTxCommandImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (HeuristicMixedException ex) {
            Logger.getLogger(CommitCurrentTxCommandImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (HeuristicRollbackException ex) {
            Logger.getLogger(CommitCurrentTxCommandImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SecurityException ex) {
            Logger.getLogger(CommitCurrentTxCommandImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalStateException ex) {
            Logger.getLogger(CommitCurrentTxCommandImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SystemException ex) {
            Logger.getLogger(CommitCurrentTxCommandImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "";
    }
}
